<%@page import="dao.UserManager"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Virtual Intranet Library</title>
</head>
<body>
	<%@include file="home.jsp"%>
	<h2>Edit profile</h2>
	<%
		user.clearErrors();
		if (request.getParameter("submit") != null) {
			String previousName = user.getName();
			String previousSurname = user.getSurname();
			String previousEmail = user.getEmail();
			String previousWorkplace = user.getWorkplace();
			user.setName(request.getParameter("name"));
			user.setSurname(request.getParameter("surname"));
			user.setEmail(request.getParameter("email"));
			user.setWorkplace(request.getParameter("workplace"));
			if(user.validate(false)){
				boolean result = UserManager.updateUser(user);
				if (result) {
					out.println("<h3>Profile changed successfully.</h3>");
				} else {
					out.println("<h3>There was a problem changing your profile.</h3>");
				}
			}
			else{
				user.setName(previousName);
				user.setSurname(previousSurname);
				user.setEmail(previousEmail);
				user.setWorkplace(previousWorkplace);
			}
		}
	%>
	<form method="post">
		Name: <input type="text" name="name" value="<%= user.getName()%>"> <%= user.getError("name") %><br>
		Surname: <input type="text" name="surname" value="<%= user.getSurname()%>"> <%= user.getError("surname") %><br>
		Email: <input type="text" name="email" value="<%= user.getEmail()%>"> <%= user.getError("email") %><br>
		Workplace: <input type="text" name="workplace" value="<%= user.getWorkplace()%>"> <%= user.getError("workplace") %><br>
		<input type="submit" name="submit" value="Change">
	</form>
</body>
</html>